package com.util.dateutil;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: yangzhiwei
 * @Date: 2021/1/25 21:07
 */
public class DateUtil {

    /**
     * 这是一个线程安全的方法
     * 通过指定 时间字符串的 原格式，目标格式，期望对时间增长的天数
     * @param strTime
     * @param plusDay it can be negative
     * @param sourceFormat
     * @param targetFormat
     * @return
     */
    public static String formatTranslate(String strTime,int plusDay,String sourceFormat,String targetFormat) {
        DateTimeFormatter format = DateTimeFormat.forPattern(sourceFormat);
        DateTime dateTime = DateTime.parse(strTime,format);
        dateTime.plusDays(plusDay);
        return dateTime.toString(targetFormat);
    }

    /**
     * You should guarantee that the startDate before the endDate
     * @param startDate
     * @param endDate
     * @param sourcePattern
     * @param targetPattern
     * @return
     */
    public static List<String> getDays(String startDate, String endDate, String sourcePattern,String targetPattern) {
        List<String> res = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormat.forPattern(sourcePattern);
        LocalDate sDate = LocalDate.parse(startDate,formatter);
        LocalDate eDate = LocalDate.parse(endDate,formatter);
        System.out.println(sDate.isBefore(eDate));
        while (!sDate.isAfter(eDate)) {
            res.add(sDate.toString(targetPattern));
            sDate = sDate.plusDays(1);
        }
        return res;
    }

    public static List<String> getDays(String startDate, String endDate) {
       return  getDays(startDate,endDate,"yyyy-MM-dd","yyyy-MM-dd");
    }
}
